package com.it.gw.modules.station.activity

import DictVo
import DictVoView
import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.google.android.flexbox.FlexboxLayout
import com.it.common.bean.RtuData
import com.it.common.utils.DialogUtils
import com.it.common.utils.GsonUtils
import com.it.common.utils.StringUtils
import com.it.common.utils.isNotEmpty
import com.it.data.network.rsp.GQYWXXVo
import com.it.data.network.rsp.MyWorkOrderVo
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.data.DynamicObject
import com.it.gw.databinding.ActivityGqOrderStationInfoBinding
import com.it.gw.databinding.DialogGwMoreInfoBinding
import com.it.gw.databinding.ItemGqStationInfoBinding
import com.it.gw.databinding.ItemGqStationLocationBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.manager.DictionariesManager
import kotlinx.coroutines.launch


//测站基本信息,我的工单
class GQOrderStationInfoActivity : BaseActivity<ActivityGqOrderStationInfoBinding>() {

    private fun showSelectWlanDialog(vo: DictVoView, textView: TextView) {
        val list = mutableListOf<DictVo>()
        list.add(DictVo(dataCode = "2G", dataName = "2G"))
        list.add(DictVo(dataCode = "3G", dataName = "3G"))
        list.add(DictVo(dataCode = "4G", dataName = "4G"))
        list.add(DictVo(dataCode = "5G", dataName = "5G"))
        list.add(DictVo(dataCode = "6G", dataName = "6G"))
        list.add(DictVo(dataCode = "卫星", dataName = "卫星"))
        showSelectWheelViewDialog(okAction = { index ->
            Log.d(TAG, "selected: ${list?.get(index)}")
            vo.dataValue = list?.get(index)?.dataName

            textView.text = vo.dataValue

        }, dictVoList = list)

    }

    private fun toShowYearMonthDialog(vo: DictVoView, textView: TextView) {
        showYearMonthWheelViewDialog(okAction = { year, month ->
            Log.d(TAG, "toShowYearMonthDialog: ${year} and ${month} ${textView}")
            val finalYear = year.replace("年", "")
            val finalMonth = month.replace("月", "")
            vo.dataValue = "$finalYear$finalMonth"

            textView.text = vo.dataValue
        })
    }

    private fun showSelectModel(vo: DictVoView, textView: TextView) {
        Log.d(TAG, "showSelectModel: showSelectModel ${vo}")
        ProgressDialog.show(this)
        gqStationViewModel.getDictName(vo.model!!) {
            ProgressDialog.hide()
            Log.d(TAG, "getDictName: getDictName ${it.data}")
            if (it.data.isNotEmpty()) {
                showSelectWheelViewDialog(okAction = { index ->
                    Log.d(TAG, "selected: ${it.data?.get(index)}")
                    vo.dataValue = it.data?.get(index)!!.dataCode
                    textView.text = it.data?.get(index)!!.dataName

//                    lifecycleScope.launch {
//                        ProgressDialog.show(this@GQStationInfoActivity)
//                        val newValue = gqStationViewModel.getDicValueByModelAndCode(
//                            vo.model!!,
//                            vo.dataValue!!
//                        )
//                        ProgressDialog.hide()
//
//                    }

                }, dictVoList = it.data!!)
            }

        }
    }

    companion object {
        const val TAG = "GQStationInfoActivity"
    }

    var mGQYWXXVo: GQYWXXVo? = null
    var myWorkOrderVo: MyWorkOrderVo? = null

    @SuppressLint("SetTextI18n")
    override fun initObserver() {
        gqStationViewModel.myWorkOrderVoLiveData.observe(this) { v ->
            ProgressDialog.hide()
           if(v.result){
               myWorkOrderVo = v.data
               binding.startTimeEndTime.text =
                   "任务时间:${StringUtils.getNoNullData(v.data?.startTime)}/${
                       StringUtils.getNoNullData(
                           v.data?.endTime
                       )
                   }"
               binding.createdTime.text =
                   "派单时间:${StringUtils.getNoNullData(v.data?.createdTime)}"
               if (v.data?.status == "1") {
                   binding.rlQUEDEAL.setBackgroundResource(R.drawable.shape_right_green)
                   binding.QUEDEAL.text = "已\n\n完\n\n成"
                   binding.btnReport.isEnabled = false
                   binding.btnReport.setBackgroundResource(R.drawable.shape_dark)
                   binding.tvUpdate.isEnabled = false
                   binding.tvUpdate.visibility = View.INVISIBLE
               } else {
                   binding.rlQUEDEAL.setBackgroundResource(R.drawable.shape_right_yellow)
                   binding.QUEDEAL.text = "未\n\n完\n\n成"
                   binding.btnReport.isEnabled = true
                   binding.btnReport.setBackgroundResource(R.drawable.shape_btn_red)
                   binding.tvUpdate.isEnabled = true
                   binding.tvUpdate.visibility = View.VISIBLE

               }
               binding.orderType.text =
                   "任务类型:${ DictionariesManager.orderTypeList.find { it.first == StringUtils.getNoNullData(v.data?.orderType) }?.second}"
           }
            else{
               Toast.makeText(this, "${v.msg}", Toast.LENGTH_LONG).show()
            }


        }
        gqStationViewModel.updateGQYWXXLiveData.observe(this) {
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "修改成功", Toast.LENGTH_LONG).show()
                isUpdate = false
                setUpdateView()
            } else {
                Toast.makeText(this, "修改失败 ${it.msg}", Toast.LENGTH_LONG).show()
            }

        }
        mainViewModel.gqywxxVoLiveData.observe(this) {
            ProgressDialog.hide()
            if(it.result){
                Log.d(
                    TAG,
                    "initObserver: gqywxxVoLiveData ${it.data?.rtuModel} -  ${it.data?.rtuModel == null} -${it.data?.rtuModel == "null"}"
                )
                val data = it.data
                mGQYWXXVo = it.data
                binding.STNM.text = StringUtils.getNoNullData(it.data?.stnm)
                binding.STCD.text = StringUtils.getNoNullData(it.data?.stcd)
                binding.ADDRESS.text = StringUtils.getNoNullData(it.data?.address)
                binding.STTP.text = StringUtils.getNoNullData("${it.data?.sttp}:${it.data?.sttpName}")

                val gqYWXXVo = it.data
                binding.comments.setText(gqYWXXVo?.comments)
                yzsf = gqYWXXVo?.yzsf
                gtcc = gqYWXXVo?.gtcc
                showSelectView()


                // 遍历集合，进行赋值
                for (item in dictVoList) {
                    // 使用反射获取 Source 对象中与 item.mode 匹配的属性
                    val property = GQYWXXVo::class.members.find {
                        Log.d(TAG, "property: ${it.name}")
                        it.name == item.model
                    }
                    Log.d(TAG, "property: $property ${item.model} ${DictVo::class.members}")
                    if (property != null) {
                        // 获取属性值并赋给 item.value
                        item.dataValue = property.call(gqYWXXVo).toString()
                        if (item.dataValue == "null") {
                            item.dataValue = null
                        }
                    }
                }
                var xdcrlModelVo = dictVoList.find { it.model == "xdcrlModel" }
                xdcrlModelVo?.dataValue = gqYWXXVo?.xdcRl

                var tynbglModellVo = dictVoList.find { it.model == "tynbglModel" }
                tynbglModellVo?.dataValue = gqYWXXVo?.tynbgl

                val lsssTypeVo = dictVoList.find { it.model == "LSSS_TYPE" }
                Log.d(TAG, "lsssTypeVo: ===${lsssTypeVo}")
                lsssTypeVo?.dataValue = gqYWXXVo?.lsssType



                dictVoList.forEach { d ->
                    if (d.model == "location") {
                        d.itemGqStationLocationBinding =
                            ItemGqStationLocationBinding.inflate(layoutInflater)
                    } else {
                        d.itemGqStationInfoBinding = ItemGqStationInfoBinding.inflate(layoutInflater)
                    }

                }
                dictVoList.forEach { d ->
                    if (d.model == "location") {
                        addLocationItemToFlexboxLayout(d)
                    } else {
                        addItemToFlexboxLayout(d)
                    }
                    Log.d(
                        TAG,
                        "forEach:${d.model} ${d.dataValue} ${d.dataValue == null} ${d.dataValue == "null"}"
                    )

                }

                isUpdate = false
                setUpdateView()
                binding.btnPic.isEnabled = true
                binding.btnRecord.isEnabled = true
                //binding.btnReport.isEnabled = true
                ProgressDialog.show(this)
                getOrderDetailById()
            }
            else{
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_LONG).show()
            }

        }

    }

    private fun showSelectView() {
        if (yzsf == "1") {
            binding.ivYjsfYes.setImageResource(R.drawable.icon_selected)
            binding.ivYjsfNo.setImageResource(R.drawable.icon_un_select)
        } else {
            binding.ivYjsfYes.setImageResource(R.drawable.icon_un_select)
            binding.ivYjsfNo.setImageResource(R.drawable.icon_selected)
        }
        Log.d(TAG, "showSelectView: ${gtcc}")
        if (gtcc == "1") {
            binding.ivGtccYes.setImageResource(R.drawable.icon_selected)
            binding.ivGtccNo.setImageResource(R.drawable.icon_un_select)
        } else {
            binding.ivGtccYes.setImageResource(R.drawable.icon_un_select)
            binding.ivGtccNo.setImageResource(R.drawable.icon_selected)
        }
    }

    var yzsf: String? = "0"
    var gtcc: String? = "0"
    override fun initListener() {
        binding.btnRecord.setOnClickListener {
            val intent = Intent(this, GQStationRecordActivity::class.java)
            intent.putExtra("stcd", mGQYWXXVo?.stcd)
            intent.putExtra("orderId", myWorkOrderVo?.id)
            startActivity(intent)
        }
        binding.btnPic.setOnClickListener {
            val intent = Intent(this, GQStationPicActivity::class.java)
            intent.putExtra("stcd", mGQYWXXVo?.stcd)
            startActivity(intent)
        }
        binding.btnReport.setOnClickListener {
            val intent = Intent(this, GQStationReportActivity::class.java)
            intent.putExtra("stcd", mGQYWXXVo?.stcd)
            intent.putExtra("orderId", myWorkOrderVo?.id)
            startActivity(intent)
        }
        binding.btnSave.setOnClickListener {
            ProgressDialog.show(this)
            // 构建动态对象
            val dynamicObject = DynamicObject()
            dictVoList.forEach {


                val model = it.model
                val dataValue = it.dataValue
                Log.d(TAG, "dynamicObject:${it.dataValue} ${it.dataValue == "null"}")
                if (model != "location") {
                    // 使用model作为属性名，dataValue作为属性值设置到动态对象
                    dynamicObject.setProperty(model, dataValue)
                }

            }
            dynamicObject.allProperties.put("yzsf", yzsf)
            dynamicObject.allProperties.put("gtcc", gtcc)
            dynamicObject.allProperties.put("comments", binding.comments.text.toString())
            dynamicObject.allProperties.forEach { t, u ->
                Log.d(TAG, "dynamicObject:${t} ${u == "null"}")
            }
            val jsonStr = GsonUtils.objToJsonStr(dynamicObject.allProperties)
            val rtu = GsonUtils.jsonStrToObj<RtuData>(jsonStr)
            mGQYWXXVo?.let {
                it.rtuModel = rtu.rtuModel
                it.esstym = rtu.esstym
                it.xdcRl = rtu.xdcrlModel
                it.lttd = rtu.lttd
                it.soil = rtu.soil
                it.sxtModel = rtu.sxtModel
                it.yzsf = rtu.yzsf
                it.rtuId = rtu.rtuId
                it.lgtd = rtu.lgtd
                it.yltModel = rtu.yltModel

                it.factory = rtu.factory
                it.address = rtu.address
                it.comments = rtu.comments
                it.dtmnm = rtu.dtmnm
                it.lljModel = rtu.lljModel
                it.stlc = rtu.stlc
                it.xdcModel = rtu.xdcModel
                it.lsssType = rtu.LSSS_TYPE
                it.dtmel = rtu.dtmel
                it.simCardnum = rtu.simCardnum

                it.cdkzqModel = rtu.cdkzqModel
                it.hnnm = rtu.hnnm
                it.wlan = rtu.wlan
                it.rvnm = rtu.rvnm
                it.tynbModel = rtu.tynbModel
                it.txmkModel = rtu.txmkModel
                it.tynbgl = rtu.tynbglModel
                it.swjModel = rtu.swjModel
                it.dtpr = rtu.dtpr
                it.gtcc = rtu.gtcc
                it.bsnm = rtu.bsnm

                gqStationViewModel.updateGQYWXX(it)
            }


        }
        binding.ivCommentsClose.setOnClickListener {
            binding.comments.setText("")
        }
        binding.llYzsfYes.setOnClickListener {
            yzsf = "1"
            showSelectView()
        }
        binding.llYzsfNo.setOnClickListener {
            yzsf = "0"
            showSelectView()
        }


        binding.llGtccYes.setOnClickListener {
            gtcc = "1"
            showSelectView()
        }
        binding.llGtccNo.setOnClickListener {
            gtcc = "0"
            showSelectView()
        }
        binding.tvUpdate.setOnClickListener {
            isUpdate = !isUpdate
            setUpdateView()


        }
        binding.tvMore.setOnClickListener {

            val dialogGwMoreInfoBinding = DialogGwMoreInfoBinding.inflate(layoutInflater)
            val dialog =
                DialogUtils.getDialog(this, dialogGwMoreInfoBinding.root)
            dialogGwMoreInfoBinding.vBack.setOnClickListener {
                dialog.dismiss()
            }
            dialogGwMoreInfoBinding.stcd.setText(StringUtils.getNoNullData(mGQYWXXVo?.stcd))
            dialogGwMoreInfoBinding.stnm.setText(StringUtils.getNoNullData(mGQYWXXVo?.stnm))
            dialogGwMoreInfoBinding.sttpName.setText(StringUtils.getNoNullData(mGQYWXXVo?.sttpName))
            dialogGwMoreInfoBinding.esstym.setText(StringUtils.getNoNullData(mGQYWXXVo?.esstym))
            dialogGwMoreInfoBinding.rvnm.setText(StringUtils.getNoNullData(mGQYWXXVo?.rvnm))
            dialogGwMoreInfoBinding.hnnm.setText(StringUtils.getNoNullData(mGQYWXXVo?.hnnm))
            dialogGwMoreInfoBinding.bsnm.setText(StringUtils.getNoNullData(mGQYWXXVo?.bsnm))
            dialogGwMoreInfoBinding.lgtd.setText(StringUtils.getNoNullData(mGQYWXXVo?.lgtd))
            dialogGwMoreInfoBinding.lttd.setText(StringUtils.getNoNullData(mGQYWXXVo?.lttd))
            dialogGwMoreInfoBinding.addvcd.setText(StringUtils.getNoNullData(mGQYWXXVo?.addvcd))
            dialogGwMoreInfoBinding.admauth.setText(StringUtils.getNoNullData(mGQYWXXVo?.admauth))
            dialogGwMoreInfoBinding.address.setText(StringUtils.getNoNullData(mGQYWXXVo?.address))
            dialog.show()
        }
        binding.copy.setOnClickListener {

            copyToClipboard(binding.STCD.text.toString())
        }


    }
    private fun copyToClipboard(text: String) {
        val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText("text", text)
        clipboardManager.setPrimaryClip(clipData)

        // 显示一个提示信息
        Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show()
    }
    var isUpdate = false
    private fun setUpdateView() {
        dictVoList.forEach {
            if (it.model == "location") {
                if (isUpdate) {
                    it.itemGqStationLocationBinding?.root?.visibility = View.VISIBLE
                } else {
                    it.itemGqStationLocationBinding?.root?.visibility = View.GONE
                }

            } else {
                if (isUpdate) {
                    it.itemGqStationInfoBinding?.ivClose?.visibility = View.VISIBLE
                    it.itemGqStationInfoBinding?.value1?.isEnabled = true
                    it.itemGqStationInfoBinding?.value2?.isEnabled = true
                } else {
                    it.itemGqStationInfoBinding?.ivClose?.visibility = View.GONE
                    it.itemGqStationInfoBinding?.value1?.isEnabled = false
                    it.itemGqStationInfoBinding?.value2?.isEnabled = false
                }


            }
        }
        if (isUpdate) {
            binding.comments.isEnabled = true
            binding.ivCommentsClose.visibility = View.VISIBLE

            binding.llYzsfYes.isEnabled = true
            binding.llYzsfNo.isEnabled = true

            binding.llGtccYes.isEnabled = true
            binding.llGtccNo.isEnabled = true

            binding.tvUpdate.text = "查看"
            binding.btnSave.visibility = View.VISIBLE
            binding.llSee.visibility = View.GONE
        } else {
            binding.ivCommentsClose.visibility = View.GONE
            binding.comments.isEnabled = false

            binding.llYzsfYes.isEnabled = false
            binding.llYzsfNo.isEnabled = false

            binding.llGtccYes.isEnabled = false
            binding.llGtccNo.isEnabled = false
            binding.tvUpdate.text = "修改"


            binding.btnSave.visibility = View.GONE
            binding.llSee.visibility = View.VISIBLE
        }
    }


    override fun initData() {


    }

    override fun onResume() {
        super.onResume()
        ProgressDialog.show(this)
        getGQStationInfo()
    }

    private fun getOrderDetailById() {
        gqStationViewModel.getOrderDetailById(intent.getStringExtra("orderDetailId")!!)
    }


    private fun getGQStationInfo() {
        val STCD = intent.getStringExtra("STCD")
        mainViewModel.getGQYWXX(STCD = STCD!!)
    }

    val dictVoList = mutableListOf<DictVoView>()
    override fun initView() {
        setTitle(
            title = R.string.str_gq_station_info,
            isBack = true,

            )
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "河流名称",
                isEdit = true,
                model = "rvnm"
            )
        )
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "水系名称",
                isEdit = true,
                model = "hnnm"
            )
        )
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "流域名称",
                isEdit = true,
                model = "bsnm"
            )
        )
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "基面名称",
                isEdit = true,
                model = "dtmnm"
            )
        )
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "基面高程",
                isEdit = true,
                model = "dtmel"
            )
        )
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "基面修正值",
                isEdit = true,
                model = "dtpr"
            )
        )
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "建设厂家",
                isEdit = true,
                model = "factory"
            )
        )
        dictVoList.add(DictVoView(dataCode = "1", dataName = "建站年月", model = "esstym"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "经度", isEdit = true, model = "lgtd"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "维度", isEdit = true, model = "lttd"))

        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "获取位置",
                isEdit = true,
                model = "location"
            )
        )


        dictVoList.add(DictVoView(dataCode = "1", dataName = "站址", isEdit = true, model = "stlc"))
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "具体地址",
                isEdit = true,
                model = "address"
            )
        )
        dictVoList.add(DictVoView(dataCode = "1", dataName = "RTU品牌型号", model = "rtuModel"))
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "rtuId",
                isEdit = true,
                model = "rtuId"
            )
        )
        dictVoList.add(DictVoView(dataCode = "1", dataName = "蓄电池品牌型号", model = "xdcModel"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "蓄电池容量", model = "xdcrlModel"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "太阳能板品牌", model = "tynbModel"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "太阳能板功率", model = "tynbglModel"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "流量计品牌型号", model = "lljModel"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "量水设施类型", model = "LSSS_TYPE"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "雨量筒型号", model = "yltModel"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "水位计品牌型号", model = "swjModel"))
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "通讯模块品牌型号",
                model = "txmkModel"
            )
        )
        dictVoList.add(DictVoView(dataCode = "1", dataName = "网络制式", model = "wlan"))
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "SIM卡号",
                isEdit = true,
                model = "simCardnum"
            )
        )
        dictVoList.add(
            DictVoView(
                dataCode = "1",
                dataName = "充电控制器品牌型号",
                model = "cdkzqModel"
            )
        )
        dictVoList.add(DictVoView(dataCode = "1", dataName = "摄像头品牌型号", model = "sxtModel"))
        dictVoList.add(DictVoView(dataCode = "1", dataName = "土壤墒情传感器", model = "soil"))


    }

    private fun addLocationItemToFlexboxLayout(vo: DictVoView) {

        Log.d(TAG, "addLocationItemToFlexboxLayout: ${vo.itemGqStationLocationBinding}")
        val itemView = vo.itemGqStationLocationBinding?.root


        val params = FlexboxLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,  // layout_width
            LinearLayout.LayoutParams.WRAP_CONTENT // layout_height
        )

        itemView?.layoutParams = params // 应用布局参数
        // 将视图添加到 FlexboxLayout
        binding.flexboxLayout.addView(itemView)
        vo.itemGqStationLocationBinding?.llLocation?.setOnClickListener {
            startLocation()

        }


    }

    private fun addItemToFlexboxLayout(vo: DictVoView) {
        vo.itemGqStationInfoBinding?.ivClose?.setOnClickListener {
            vo.dataValue = null
            vo.itemGqStationInfoBinding?.value1?.text = null
            vo.itemGqStationInfoBinding?.value2?.text = null
        }
        // 动态加载 item_gq_station_info 布局
        val itemView = vo.itemGqStationInfoBinding?.root

        setDictVoView(vo, vo.itemGqStationInfoBinding!!)
        // 设置布局参数
        val params = FlexboxLayout.LayoutParams(
            0,  // layout_width
            LinearLayout.LayoutParams.WRAP_CONTENT // layout_height
        )
        params.flexBasisPercent = 0.5f // 每个项占据父视图的一半宽度


        itemView?.layoutParams = params // 应用布局参数
        // 将视图添加到 FlexboxLayout
        binding.flexboxLayout.addView(itemView)


    }

    private fun setDictVoView(
        data: DictVoView,
        itemGqStationInfoBinding: ItemGqStationInfoBinding
    ) {
        itemGqStationInfoBinding?.name?.text = data.dataName
        var value = data.dataValue
        value = StringUtils.getNoNullData2(value)
        if (value.isNotEmpty()) {
            if (data.isEdit == true) {
                itemGqStationInfoBinding?.value1?.setText(value)
            } else {
                //年月日
                if (data.model == "esstym") {
//                    val date = DateUtils.stringToDate("yyyyMM", value)
//                    val dateStr = DateUtils.dateToString("yyyy-MM", date)
                    itemGqStationInfoBinding?.value2?.text = value
                    data.dataValue = value
                } else if (data.model == "wlan") {
                    itemGqStationInfoBinding?.value2?.text = value
                } else {
                    lifecycleScope.launch {
                        val newValue = gqStationViewModel.getDicValueByModelAndCode(
                            data.model!!,
                            value
                        )
                        itemGqStationInfoBinding?.value2?.text = newValue
                    }

                }
            }

        } else {
            if (data.isEdit == true) {
                itemGqStationInfoBinding?.value1?.setText(value)
            } else {
                itemGqStationInfoBinding?.value2?.text = value
            }
        }


        if (data.isEdit == true) {
            itemGqStationInfoBinding?.value1?.visibility = View.VISIBLE
            itemGqStationInfoBinding?.value2?.visibility = View.GONE
        } else {
            itemGqStationInfoBinding?.value1?.visibility = View.GONE
            itemGqStationInfoBinding?.value2?.visibility = View.VISIBLE
        }
        itemGqStationInfoBinding?.value2?.setOnClickListener {
            showSelectDataView(data, itemGqStationInfoBinding?.value2!!)
        }
        itemGqStationInfoBinding?.value1?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                data.dataValue = s.toString()

            }

        })

    }

    private fun showSelectDataView(vo: DictVoView, textView: TextView) {
        if (vo.isEdit == false) {
            if (vo.model == "esstym") {
                toShowYearMonthDialog(vo, textView)
            } else if (vo.model == "wlan") {
                showSelectWlanDialog(vo = vo, textView = textView)
            } else {
                showSelectModel(vo, textView)
            }

        }

    }

    override fun setContentView() {
        binding = ActivityGqOrderStationInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    //声明AMapLocationClientOption对象
    var mLocationOption: AMapLocationClientOption? = null

    //声明AMapLocationClient类对象
    var mLocationClient: AMapLocationClient? = null
    private fun startLocation() {
        Log.d(TAG, "startLocation: ===========")
        mLocationClient = AMapLocationClient(applicationContext)
        mLocationClient?.setLocationListener(object : AMapLocationListener {
            override fun onLocationChanged(p0: AMapLocation?) {
                //经度
                val lgtdVo = dictVoList.find { it.model == "lgtd" }
                lgtdVo?.dataValue = "${p0?.longitude}"
                lgtdVo?.itemGqStationInfoBinding?.value1?.setText("${p0?.longitude}")

                //维度
                val lttdVo = dictVoList.find { it.model == "lttd" }
                lttdVo?.dataValue = "${p0?.latitude}"
                lttdVo?.itemGqStationInfoBinding?.value1?.setText("${p0?.latitude}")

            }

        })

        mLocationOption = AMapLocationClientOption()
        val option = AMapLocationClientOption()
        option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn)
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setOnceLocation(true);
        option.setOnceLocationLatest(true);

        if (null != mLocationClient) {
            mLocationClient?.setLocationOption(option)
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient?.stopLocation()
            mLocationClient?.startLocation()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mLocationClient?.onDestroy();//销毁定位客户端，同时销毁本地定位服务。
    }

}